package com.example.course.controller.admin;

import com.example.course.dto.admin.*;
import com.example.course.service.AdminService;
import com.example.course.service.UserService;
import com.example.course.utils.common.enums.Code;
import com.example.course.utils.common.result.Result;
import com.example.course.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;

    @PostMapping("/admin_login")
    public Result login(@RequestBody AdminAdminLoginDto dto){
        return adminService.login(dto);
    }

    @GetMapping("/admin_login_out")
    public Result loginOut(){
        BaseContext.removeCurrentId();
        return Result.okResult(Code.SUCCESS);
    }

    @PostMapping("/save_admin")
    public Result saveAdmin(@RequestBody AdminAdminSaveDto dto){
        return adminService.saveAdmin(dto);
    }

    @PostMapping("/save_user")
    public Result saveUser(@RequestBody AdminUserSaveDto dto){
        return userService.saveUser(dto);
    }

    @PostMapping("/get_admin_list")
    public Result getAdminList(@RequestBody AdminUserListDto dto){
        return adminService.getAdminUser(dto);
    }
    @PostMapping("/get_user_list")
    public Result getUserList(@RequestBody AdminUserListDto dto){
        return userService.getUserList(dto);
    }

    @PostMapping("/remove_admin_user")
    public Result removeAdminUser(@RequestBody AdminUserRemoveDto dto){
        return adminService.removeAdminUser(dto);
    }
    @PostMapping("/remove_user")
    public Result removeUser(@RequestBody AdminUserRemoveDto dto){
        return userService.removeUser(dto);
    }

    @GetMapping("/update_get_user")
    public Result updateGetUser(@RequestParam("id")Long id){
        Long adminId = BaseContext.getCurrentId();
        if (adminId==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(userService.getById(id));
    }
    @GetMapping("/update_get_admin")
    public Result updateGetAdmin(@RequestParam("id")Long id){
        Long adminId = BaseContext.getCurrentId();
        if (adminId==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(adminService.getById(id));
    }

    @PostMapping("/update_user")
    public Result updateUser(@RequestBody AdminUpdateUserDo dto){
        return userService.updateUser(dto);
    }

    @PostMapping("/update_admin")
    public Result updateAdmin(@RequestBody AdminUpdateDto dto){
        return adminService.updateAdmin(dto);
    }

}
